﻿using Coldairarrow.Entity.View;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.View
{
    public partial class View_WorkloadStatisticsBusiness : BaseBusiness<View_WorkloadStatistics>, IView_WorkloadStatisticsBusiness, ITransientDependency
    {
        public View_WorkloadStatisticsBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<View_WorkloadStatistics>> GetDataListAsync(PageInput<WorkloadConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<View_WorkloadStatistics>();
            var search = input.Search;

            //筛选
            if (!search.Keyword.IsNullOrEmpty())
            {
                where = where.And(x => search.Keyword.Contains(x.RealName));
            }
            if (!search.DepartmentId.IsNullOrEmpty())
            {
                where = where.And(x => x.DepartmentId == search.DepartmentId);
            }
            if (!search.ProcedureId.IsNullOrEmpty())
            {
                where = where.And(x => x.ProcedureId == search.ProcedureId);
            }
            if (!search.StartDate.IsNullOrEmpty() )
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_WorkloadStatistics, bool>(
                    ParsingConfig.Default, false, $@" SendTime>=(@0)", search.StartDate);
                where = where.And(newWhere);
            }
            if (!search.EndDate.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_WorkloadStatistics, bool>(
                   ParsingConfig.Default, false, $@" SendTime<=(@0)", search.EndDate);
                where = where.And(newWhere);
            }
            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<List<View_WorkloadStatistics>> GetTheDataListAsync(WorkloadConditionDTO input)
        {
            var where = LinqHelper.True<View_WorkloadStatistics>();
            //筛选
            if (!input.Keyword.IsNullOrEmpty())
            {
                where = where.And(x => input.Keyword.Contains(x.RealName));
            }
            if (!input.DepartmentId.IsNullOrEmpty())
            {
                where = where.And(x => x.DepartmentId == input.DepartmentId);
            }
            if (!input.ProcedureId.IsNullOrEmpty())
            {
                where = where.And(x => x.ProcedureId == input.ProcedureId);
            }
            if (!input.StartDate.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_WorkloadStatistics, bool>(
                    ParsingConfig.Default, false, $@" SendTime>=(@0)", input.StartDate);
                where = where.And(newWhere);
            }
            if (!input.EndDate.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<View_WorkloadStatistics, bool>(
                   ParsingConfig.Default, false, $@" SendTime<=(@0)", input.EndDate);
                where = where.And(newWhere);
            }
            return await Db.GetIQueryable<View_WorkloadStatistics>().Where(where).ToListAsync();

        }

        public async Task<View_WorkloadStatistics> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(View_WorkloadStatistics data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(View_WorkloadStatistics data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}